.TH std::exchange 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::exchange \- std::exchange

.SH Synopsis
   Defined in header <utility>
   template< class T, class U = T >                                       \fI(since C++14)\fP
   T exchange( T& obj, U&& new_value );                                   \fI(until C++20)\fP
   template< class T, class U = T >                                       \fI(since C++20)\fP
   constexpr T exchange( T& obj, U&& new_value );                         (until C++23)
   template< class T, class U = T >
   constexpr T exchange( T& obj, U&& new_value ) noexcept(/* see below    (since C++23)
   */);

   Replaces the value of obj with new_value and returns the old value of obj.

.SH Parameters

   obj                    -              object whose value to replace
   new_value              -              the value to assign to obj
.SH Type requirements
   -
   T must meet the requirements of MoveConstructible. Also, it must be possible to
   move-assign objects of type U to objects of type T.

.SH Return value

   The old value of obj.

.SH Exceptions

   \fI(none)\fP                                         (until C++23)
   noexcept specification:
   noexcept(

       std::is_nothrow_move_constructible_v<T> && (since C++23)
       std::is_nothrow_assignable_v<T&, U>

   )

.SH Possible implementation

   template<class T, class U = T>
   constexpr // Since C++20
   T exchange(T& obj, U&& new_value)
       noexcept( // Since C++23
           std::is_nothrow_move_constructible<T>::value &&
           std::is_nothrow_assignable<T&, U>::value
       )
   {
       T old_value = std::move(obj);
       obj = std::forward<U>(new_value);
       return old_value;
   }

.SH Notes

   The std::exchange can be used when implementing move assignment operators and move
   constructors:

 struct S
 {
     int n;

     S(S&& other) noexcept : n{std::exchange(other.n, 0)} {}

     S& operator=(S&& other) noexcept
     {
         n = std::exchange(other.n, 0); // Move n, while leaving zero in other.n
                                        // (note: in self-move-assignment, n is unchanged)
         return *this;
     }
 };

       Feature-test macro       Value    Std      Feature
   __cpp_lib_exchange_function 201304L \fI(C++14)\fP std::exchange

.SH Example


// Run this code

 #include <iostream>
 #include <iterator>
 #include <utility>
 #include <vector>

 class stream
 {
 public:
     using flags_type = int;

 public:
     flags_type flags() const { return flags_; }

     // Replaces flags_ by newf, and returns the old value.
     flags_type flags(flags_type newf) { return std::exchange(flags_, newf); }

 private:
     flags_type flags_ = 0;
 };

 void f() { std::cout << "f()"; }

 int main()
 {
     stream s;

     std::cout << s.flags() << '\\n';
     std::cout << s.flags(12) << '\\n';
     std::cout << s.flags() << "\\n\\n";

     std::vector<int> v;

     // Since the second template parameter has a default value, it is possible
     // to use a braced-init-list as second argument. The expression below
     // is equivalent to std::exchange(v, std::vector<int>{1, 2, 3, 4});

     std::exchange(v, {1, 2, 3, 4});

     std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, ", "));

     std::cout << "\\n\\n";

     void (*fun)();

     // The default value of template parameter also makes possible to use a
     // normal function as second argument. The expression below is equivalent to
     // std::exchange(fun, static_cast<void(*)()>(f))
     std::exchange(fun, f);
     fun();

     std::cout << "\\n\\nFibonacci sequence: ";
     for (int a{0}, b{1}; a < 100; a = std::exchange(b, a + b))
         std::cout << a << ", ";
     std::cout << "...\\n";
 }

.SH Output:

 0
 0
 12

 1, 2, 3, 4,

 f()

 Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

.SH See also

   swap                     swaps the values of two objects
                            \fI(function template)\fP
   atomic_exchange          atomically replaces the value of the atomic object with
   atomic_exchange_explicit non-atomic argument and returns the old value of the atomic
   \fI(C++11)\fP                  \fI(function template)\fP
   \fI(C++11)\fP

.SH Category:
     * conditionally noexcept
